package net.blogways.ordermgr.common.service;

import com.asiainfo.bpc.exception.BusiException;
import org.slf4j.Logger;
import com.asiainfo.bpc.utils.ToolFactory;
import net.blogways.ordermgr.util.Constants;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenlong
 * Created on 2018/3/5
 */
@Service
public class CommonService {

  private static final Logger log = LoggerFactory.getLogger(CommonService.class);

  /**
   * 获取静态参数表中的数据.
   * @param paramAttr
   * @param paramCode
   * @return
   * @throws Exception
   */
//  @Cacheable(value = "ehCache", key = "'staticCommpara' + '_' + #paramAttr + '_' + #paramCode + '_' + #local")
  public List getCommpara(String paramAttr, String paramCode, String local) throws Exception{
    log.debug("getBatchCode begin : ");
    Map map = new HashMap();
    map.put("PARAM_ATTR", paramAttr);
    map.put("PARAM_CODE", paramCode);
    map.put("LOCALE", local == null ? "zh" : local);
    List rs = ToolFactory.getInstance().getSqlCodeService(Constants.DATABASE_CEN)
            .select("SQL_SEL_STATIC_PARAM", "TD_S_COMMPARA", map);
    if (rs.size() != 1){
      log.debug("No Record In Table.");
      log.debug("getBatchCode End : ****************");
      return null;
    }
    return rs;
  }

//  @Cacheable(value = "ehCache", key = "'cacheParam_' + #tableName + '_' + #code + '_' + #typeName",
//          unless = "#tableName.contains('TF_F_')")
  public Map getTypeNameByCode(String tableName, String code, String typeName, String locale, String dbRoute)
          throws Exception {
    log.debug("tableName={},typeName={},code={},dbRoute={}", tableName, typeName, code, dbRoute);

    if (!code.contains(":")) {
      throw new BusiException("E009", "the code param of code2Name must contains ':'!");
    }

    if ("".equals(dbRoute)){
      dbRoute = Constants.DATABASE_CEN;
    }
    Map result = new HashMap();
    if (code == null || code.isEmpty()){
      return result;
    }
    String[] codeArr = code.split(",");
    StringBuffer buffer = new StringBuffer();
    Map param = new HashMap<>();
    buffer.append("SELECT * FROM ");
    buffer.append(tableName.toString());
    buffer.append(" where ");
    for (int i = 0; i < codeArr.length; i++){
      String colVal = codeArr[i].split(":")[1]; // typeCode column name
      String colName = codeArr[i].toString().split(":")[0]; // typeCode value
      if (i > 0){
        buffer.append(" AND ");
      }
      buffer.append(colName);
      buffer.append(" = :");
      buffer.append(colName);
      param.put(colName, colVal);
    }
    if (!"".equals(locale)) {
      buffer.append(" AND LOCALE = :locale ");
      param.put("locale", locale);
    }
    log.debug(buffer.toString());
    List list = ToolFactory.getInstance().getSqlCodeService(dbRoute).select(buffer.toString(), param);

    if (list.size() > 0) {
      Map rsMap = (Map) list.get(0);

      String[] nameArr = typeName.split(",");

      for (int j = 0; j < nameArr.length; j++) {
        result.put(nameArr[j], rsMap.get(nameArr[j]));
      }
    }

    return result;
  }

//  @Cacheable(value = "ehCache", key = "'dictCode' + '_' + #typeCode + '_' + #code")
  public String dictCode2Name(String typeCode, String code, String locale) throws Exception {
    log.debug("typeCode={},code={}", typeCode, code);

    String retTypeName = "";
    Map param = new HashMap<>();
    param.put("VDICT_CODE", code);
    param.put("VDICT_TYPE_CODE", typeCode);
    param.put("VLOCALE", locale);
    List list = ToolFactory.getInstance().getSqlCodeService(Constants.DATABASE_CEN)
            .select("SEL_TD_S_DATA_DICT_NAME_BY_CODE", "TD_S_DATA_DICT", param);
    log.debug("Result={} ", list.toString());

    if (!list.isEmpty()) {
      retTypeName = ((Map) list.get(0)).get("TYPE_NAME").toString();
    }
    return retTypeName;
  }



}
